﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

namespace ProductsWeb
{
    /// <summary>
    /// Summary description for Products
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Products : System.Web.Services.WebService
    {
        public List<Product> productList;
        public List<Category> categoryList;

        public Products() {
            //Build Product List
            productList = new List<Product>();
            productList.Add(new Product()
            {
                ID = 1,
                ProductName = "Nokia Lumia 930",
                ProductDescription = "Windows Phone 8.1 with Charging Cover",
                ProductImage = "http://product77.blob.core.windows.net/images/p1.jpg",
                CategoryID = 1
            });
            productList.Add(new Product() {
                ID = 2,
                ProductName = "iPhone 6S",
                ProductDescription = "Apple Phone with 5.5 inch screen ",
                ProductImage = "http://product77.blob.core.windows.net/images/p2.jpg",
                CategoryID = 2
            });
        }

        public List<Product> getProductListFromAzure() {
            string str_connection = "Server=tcp:yver2m1c9t.database.windows.net,1433;Database=Product;User ID=pongsakorn@yver2m1c9t;Password=wePA8921!@#;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;";
            SqlConnection con = new SqlConnection(str_connection);
            con.Open();
            SqlCommand command = con.CreateCommand();
            command.CommandText = "select ID,ProductName,ProductDescription,"+
                "ProductImage, CategoriesID from Products";
            SqlDataReader reader  =  command.ExecuteReader();
            productList.Clear();
            while (reader.Read())
            {
                string cnd_url = reader.GetString(3).Replace("http://product77.blob.core.windows.net",
                                                                "http://az630315.vo.msecnd.net");
                productList.Add(new Product() { 
                            ID=reader.GetInt32(0),
                            ProductName = reader.GetString(1),
                            ProductDescription =  reader.GetString(2),
                            ProductImage = cnd_url,
                            CategoryID = reader.GetInt32(4)
                });
            }
            con.Close();
            return productList;
        }
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public double Add(double a, double b)
        {
            return a+b;
        }

        [WebMethod]
        public List<Product> getProductList()
        {
            return this.getProductListFromAzure();
        }
    }
}
